package com.allen.retrofit2_rxjava2.view.calendar;

import android.content.Intent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.base.TitleViewActivity;
import com.allen.retrofit2_rxjava2.bean.CalendarData;
import com.allen.retrofit2_rxjava2.bean.Day;
import com.allen.retrofit2_rxjava2.view.TitleBarView;
import com.allen.retrofit2_rxjava2.view.calendar.adapter.CustomAdapter;
import com.allen.retrofit2_rxjava2.view.calendar.view.ChooseDateView;
import com.allen.retrofit2_rxjava2.view.calendar.view.PinnedHeaderListView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/6/14.
 */

public class CalendarActivity extends TitleViewActivity implements CustomAdapter.ChooseDataListenter {

    private Calendar calendar;
    private SimpleDateFormat sdf;
    private final int maxRange = 31;

    @BindView(R.id.textview_starttime)
    TextView textview_starttime;

    @BindView(R.id.listview)
    PinnedHeaderListView listview;

    @BindView(R.id.title_view)
    TitleBarView title_view;

    @BindView(R.id.textview_save)
    TextView textview_save;

    private CustomAdapter customAdapter;
    private List<CalendarData> data = new ArrayList<CalendarData>();
    private Day starttime;
    private ViewHandler viewHandler;
    private final int REFRESH = 1001;


    @Override //viewId的初始化都在init结束后
    public void init() {
        setContentView(R.layout.activity_calendar_choose);

    }

    @Override
    public void data() {
        initTitleBar();
        setdata();
        initView();
    }

    private void initTitleBar() {
        initTitleView();
        initLeftBackView(null);
        title_view.setCheckBox(getResources().getString(R.string.cancel));
        setCenterViewContent(getResources().getString(R.string.select_date));
    }

    private void initView() {
        if (null == viewHandler) {
            viewHandler = new ViewHandler();
        }
        View HeaderView = LayoutInflater.from(this).inflate(R.layout.listview_item_header, listview, false);
        listview.setPinnedHeader(HeaderView);
        customAdapter = new CustomAdapter(CalendarActivity.this, data);
        listview.setAdapter(customAdapter);
        listview.setOnScrollListener(customAdapter);
        customAdapter.setChooseDataListener(this);
        title_view.setRightViewOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setdata() {

        for (int i = 0; i < 12; i++) {
            calendar = Calendar.getInstance(Locale.CHINA);
            CalendarData cal = new CalendarData();
            if (null == sdf) {
                sdf = new SimpleDateFormat(getResources().getString(R.string.choosedate_format));
            }
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - i);
            cal.setCalendar(calendar);
            if (null != sdf && null != calendar) {
                cal.setTitle(sdf.format(calendar.getTime()));
            }
            data.add(cal);
        }
    }

    @Override
    public void selected(Day starttime) {
        this.starttime = starttime;
        String startText;
        if ((starttime.getMonth() + 1) < 10) {
            startText = starttime.getYear() + "年0" + (starttime.getMonth() + 1) + "月" + starttime.getDay() + "日";
        } else {
            startText = starttime.getYear() + "年" + (starttime.getMonth() + 1) + "月" + starttime.getDay() + "日";
        }
        textview_starttime.setText(startText);
        for (int j = 0; j < listview.getChildCount(); j++) {
            LinearLayout layout = (LinearLayout) listview.getChildAt(j);// 获得子item的layout
            TextView title = (TextView) layout.findViewById(R.id.title);
            ChooseDateView chooseDateView = (ChooseDateView) layout.findViewById(R.id.chooseDateView);// 从layout中获得控件,根据其id

            if (title.getText().equals(startText)) {
                chooseDateView.setStartTime(starttime);
            }

            chooseDateView.setStartTime(starttime);
            chooseDateView.setEndTime(null);

            chooseDateView.invalidate();
        }
        customAdapter.notifyDataSetChanged();

        if (null != viewHandler) {
            viewHandler.sendEmptyMessage(REFRESH);
        }
    }

    @Override
    public void selected(Day starttime, Day endtime) {
//        if (null != starttime) {
//            this.starttime = starttime;
//            String startText;
//            String endText = "";
//            if ((starttime.getMonth() + 1) < 10) {
//                startText = starttime.getYear() + "年0" + (starttime.getMonth() + 1) + "月" + starttime.getDay() + "日";
//            } else {
//                startText = starttime.getYear() + "年" + (starttime.getMonth() + 1) + "月" + starttime.getDay() + "日";
//            }
//            textview_starttime.setText(startText);
//            if (null != endtime) {
//                this.endtime = endtime;
//                if ((endtime.getMonth() + 1) < 10) {
//                    endText = endtime.getYear() + "年0" + (endtime.getMonth() + 1) + "月" + endtime.getDay() + "日";
//                } else {
//                    endText = endtime.getYear() + "年" + (endtime.getMonth() + 1) + "月" + endtime.getDay() + "日";
//                }
//
//                try {
//                    SimpleDateFormat sdfOne = new SimpleDateFormat(
//                            getResources().getString(R.string.date_format_year2day));
//                    long chooseStartTime = sdfOne.parse(startText).getTime() / 1000;
//                    long chooseEndtime = sdfOne.parse(endText).getTime() / 1000;
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//
//            } else {
//                this.endtime = null;
//                textview_save.setEnabled(false);
//                textview_save.setBackgroundResource(R.color.grey_cccccc);
//
//            }
//
//            for (int j = 0; j < listview.getChildCount(); j++) {
//                LinearLayout layout = (LinearLayout) listview.getChildAt(j);// 获得子item的layout
//                TextView title = (TextView) layout.findViewById(R.id.title);
//                ChooseDateView chooseDateView = (ChooseDateView) layout.findViewById(R.id.chooseDateView);// 从layout中获得控件,根据其id
//
//                if (title.getText().equals(startText)) {
//                    chooseDateView.setStartTime(starttime);
//                    chooseDateView.setEndTime(endtime);
//
//                }
//                if (!TextUtils.isEmpty(endText)) {
//                    if (title.getText().equals(endText)) {
//                        chooseDateView.setStartTime(starttime);
//                        chooseDateView.setEndTime(endtime);
//
//                    }
//                } else {
//                    chooseDateView.setStartTime(starttime);
//                    chooseDateView.setEndTime(null);
//
//                }
//                chooseDateView.invalidate();
//            }
//            customAdapter.notifyDataSetChanged();
//        }

    }

    @OnClick(value = {R.id.textview_save})
    public void clickBtn(View view) {
        switch (view.getId()) {
            case R.id.textview_save:
                Intent intent = getIntent();
                intent.putExtra("startTime", starttime);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void clean(Day starttime) {
        if (null != starttime) {
            textview_starttime
                    .setText(starttime.getYear() + "年" + (starttime.getMonth() + 1) + "月" + starttime.getDay() + "日");

        }
    }

    @Override
    public void error(int i) {
        if (i == 0) {
            showToast(getResources().getString(R.string.choosedate_error8) + maxRange
                    + getResources().getString(R.string.choosedate_error7));
        }
        if (i == 1) {
            showToast(getResources().getString(R.string.choosedate_error5));
        }

    }


    public class ViewHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REFRESH:
                    if (null != starttime) {
                        textview_save.setEnabled(true);
                        textview_save.setBackgroundResource(R.color.yellow_view);
                    } else {
                        textview_save.setEnabled(false);
                        textview_save.setBackgroundResource(R.color.grey_cccccc);
                    }
                    break;

            }
        }

    }
}